﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_TicketBuy : System.Web.UI.Page
{
	private Int64 TripID { get { return DS.ToInt64(Request.QueryString["id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			InitControls();
		}
	}

	private int Comparer(RouteStationM2M rs1, RouteStationM2M rs2)
	{
		if (rs1.Distance > rs2.Distance)
			return 1;
		if (rs1.Distance < rs2.Distance)
			return -1;
		return 0;
	}

	protected void InitControls()
	{
		Trip trip = DBEntity<Trip>.Load(TripID);
		if (null != trip)
		{
			int placesCount = 0;
			if (trip.Bus_id > 0)
			{
				Bus bus = DBEntity<Bus>.Load(trip.Bus_id);
				placesCount = bus.PlaceCount;
				lblTicketsCount.Text = bus.PlaceCount.ToString();
			}

			RouteStationM2M rs = new RouteStationM2M();
			rs.Route_id = trip.Route_id;
			List<RouteStationM2M> stationsInfo = DBEntity<RouteStationM2M>.Select(rs).ToList();
			stationsInfo.Sort(Comparer);

			List<string> stationsNames = (from s in stationsInfo
										  select DBEntity<Station>.Load(s.Station_id).Name).ToList();

			ddlBeginStation.DataSource = stationsNames;
			ddlBeginStation.DataBind();

			string startName = ddlBeginStation.SelectedItem.Value.ToString();

			Ticket ticket = new Ticket();
			ticket.Trip_id = TripID;
			IList<Ticket> selledTickets = DBEntity<Ticket>.Select(ticket);

			var ticketsNumbers = from t in selledTickets
								 where (stationsNames.IndexOf(DBEntity<Station>.Load(t.SourceStation_id).Name) <= stationsNames.IndexOf(startName) &&
										stationsNames.IndexOf(DBEntity<Station>.Load(t.TargetStation_id).Name) > stationsNames.IndexOf(startName))
								 select t.Place.ToString();
			string selledPlaces = String.Empty;
			foreach (var t in ticketsNumbers)
				selledPlaces += t + ", ";
			selledPlaces = selledPlaces.Trim().TrimEnd(',');
			lblSelledTickets.Text = selledPlaces;
			ddlAvailablePlaces.Items.Clear();
			for (int i = 0; i < placesCount; i++)
				if (!ticketsNumbers.Contains(i.ToString()))
					ddlAvailablePlaces.Items.Add(new ListItem(i.ToString(), i.ToString()));
			lblAvailablePlacesCount.Text = ddlAvailablePlaces.Items.Count.ToString();

			if (stationsNames.Contains(startName))
			{
				while(stationsNames[0] != startName)
					stationsNames.Remove(stationsNames[0]);
				stationsNames.Remove(startName);
			}

			ddlEndStation.DataSource = stationsNames;
			ddlEndStation.DataBind();

		}
	}

	protected void ddlBeginStation_SelectedIndexChanged(object sender, EventArgs e)
	{
		Trip trip = DBEntity<Trip>.Load(TripID);
		if (null != trip)
		{
			int placesCount = 0;
			if (trip.Bus_id > 0)
			{
				Bus bus = DBEntity<Bus>.Load(trip.Bus_id);
				placesCount = bus.PlaceCount;
				lblTicketsCount.Text = bus.PlaceCount.ToString();
			}

			RouteStationM2M rs = new RouteStationM2M();
			rs.Route_id = trip.Route_id;
			List<RouteStationM2M> stationsInfo = DBEntity<RouteStationM2M>.Select(rs).ToList();
			stationsInfo.Sort(Comparer);

			List<string> stationsNames = (from s in stationsInfo
										  select DBEntity<Station>.Load(s.Station_id).Name).ToList();

			string startName = ddlBeginStation.SelectedItem.Text ?? "";

			Ticket ticket = new Ticket();
			ticket.Trip_id = TripID;
			IList<Ticket> selledTickets = DBEntity<Ticket>.Select(ticket);

			var ticketsNumbers = from t in selledTickets
								 where (stationsNames.IndexOf(DBEntity<Station>.Load(t.SourceStation_id).Name) <= stationsNames.IndexOf(startName) &&
										stationsNames.IndexOf(DBEntity<Station>.Load(t.TargetStation_id).Name) > stationsNames.IndexOf(startName))
								 select t.Place.ToString();
			string selledPlaces = String.Empty;
			foreach (var t in ticketsNumbers)
				selledPlaces += t + ", ";
			selledPlaces = selledPlaces.Trim().TrimEnd(',');
			lblSelledTickets.Text = selledPlaces;
			ddlAvailablePlaces.Items.Clear();
			for (int i = 0; i < placesCount; i++)
				if (!ticketsNumbers.Contains(i.ToString()))
					ddlAvailablePlaces.Items.Add(new ListItem(i.ToString(), i.ToString()));
			lblAvailablePlacesCount.Text = ddlAvailablePlaces.Items.Count.ToString();

			//if (stationsNames.Contains(startName))
			//    stationsNames.Remove(startName);
			if (stationsNames.Contains(startName))
			{
				while (stationsNames[0] != startName)
					stationsNames.Remove(stationsNames[0]);
				stationsNames.Remove(startName);
			}

			ddlEndStation.DataSource = stationsNames;
			ddlEndStation.DataBind();
		}
	}

	protected void btnBuy_Click(object sender, EventArgs e)
	{
		Response.Redirect("~/Pages/PayTicket.aspx");
	}
}